package com.wy2.eclipse.tool.logger;


import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.wy2.core.tool.DatetimeTools;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.StackTraceElementProxy;


@SuppressWarnings("restriction")
public class ToolSuiteConsoleAppenderImpl {
    
    
  protected void append(ILoggingEvent logEvent) {
	  ToolSuiteConsole console=LogActivator.getDefault().getDefaultConsole();
    switch(logEvent.getLevel().levelInt) {
      case Level.DEBUG_INT:
    	  console.info(formatMessage(logEvent));
        return;
      case Level.ERROR_INT:
    	  console.error(formatMessage(logEvent));
        return;
      case Level.WARN_INT:
      case Level.INFO_INT:
      default:
    	  console.info(formatMessage(logEvent));
        return;
    }
  }
 
  private String formatMessage(ILoggingEvent logEvent) {
	  String level=logEvent.getLevel().levelStr;
	  String threadName=logEvent.getThreadName();
	  String clazzName= logEvent.getLoggerName();
	  String[] clzs=clazzName.split("\\.");
	  int len=clzs.length;
	  StringBuilder sbClazzName=new StringBuilder();
	  for(int i=0;i<len-1;i++) {
		  sbClazzName.append(clzs[i].substring(0, 1));
		  sbClazzName.append(".");
	  }
	  sbClazzName.append(clzs[len-1]);
	  clazzName=sbClazzName.toString();
	  
	  String msg=logEvent.getMessage();
	  Object[] args = logEvent.getArgumentArray();
	  IThrowableProxy throwable = logEvent.getThrowableProxy();
	  if(throwable!=null) {
		  StringBuilder sb=new StringBuilder();
		  sb.append(MessageFormat.format("{0} \r", msg));
		  sb.append(MessageFormat.format("{0} : {1}\r", throwable.getClassName(),throwable.getMessage()));
		  StackTraceElementProxy[] eles = throwable.getStackTraceElementProxyArray();
		  for (StackTraceElementProxy stackTraceElementProxy : eles) {
			  sb.append(MessageFormat.format("{0}{1}\r", "\t",stackTraceElementProxy.getSTEAsString()));
		  }
		  msg=sb.toString();
	  }else if(msg.contains("{}") && args!=null && args.length>0) {
		  Pattern pattern = Pattern.compile("\\{\\}");
		  Matcher matcher = pattern.matcher(msg);
		  int index=0,pos=0,count;
		  StringBuilder resultSb=new StringBuilder();
		  
		  while(matcher.find()) {
			    resultSb.append(msg.substring(pos,matcher.start()));
				resultSb.append("{");
				resultSb.append(String.valueOf(index));
				resultSb.append("}");
				pos=matcher.end();
				index++;
			}
			if (pos < msg.length()) {
				resultSb.append(msg.substring(pos));
			}
		  msg=MessageFormat.format(resultSb.toString(), args);
	  }
	  return String.format("%s [%s] %s %s - %s",DatetimeTools.getCurrentDatetime(),threadName,level,clazzName,msg);
	  //return logEvent.getFormattedMessage();//String.format("[%s] %s %s",logEvent.getLevel().levelStr,logEvent.getFormattedMessage(), logEvent.getMessage());
  }
}
